﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSBusiness.InterFace;
using CARMSData.EntityClasses;

namespace CARMS.ReportSetting
{
    [Function("Admin.ReportPropertyDetail")]
    public partial class ReportPropertyDetail : PageBase
    {
        private int CurrentPropertyID
        {
            get
            {
                return GetIntByParameter("PropertyID");
            }
        }

        private int CurrentReportCategoryID
        {
            get
            {
                return GetIntByParameter("ReportCategoryID");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDefault();
                LoadDefault();
            }
        }
        private void BindDefault()
        {
            ControlHelper.BindPublicItemsByTypeID(dplPropertyType, DictionaryCategoryConst.GridColumnTypeID, EmptyType.None);
            ControlHelper.BindDictionaryCategory(dplPulicCategory);
        }



        protected void dplPropertyType_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (dplPropertyType.SelectedValue == DictionaryCategoryConst.GridColumnType.DictionaryItem.ToString())
            {

                dplPulicCategory.Visible = true;
            }
            else
            {
                dplPulicCategory.Visible = false;
            }

            if (dplPropertyType.SelectedValue == DictionaryCategoryConst.GridColumnType.RelativeTable.ToString())
            {
                panelRelativeTable.Visible = true;
            }
            else
            {
                panelRelativeTable.Visible = false;
            }
        }


        private void LoadDefault()
        {
            var PropertyEntity = new ReportPropertyEntity(CurrentPropertyID);
            if (!PropertyEntity.IsNew)
            {
                txtPropertyName.Text = PropertyEntity.PropertyName;
                lblCreateDate.Text = PropertyEntity.CreateDate.ToString(ApplicationConfiguration.DateTimeFormat);
                txtHeaderText.Text = PropertyEntity.Title;
                dplPropertyType.SelectedValue = PropertyEntity.PropertyType.ToString();

                if (PropertyEntity.PropertyDictCategoryId.HasValue)
                {
                    dplPulicCategory.SelectedValue = PropertyEntity.PropertyDictCategoryId.Value.ToString();
                }

                txtRelativeTableName.Text = PropertyEntity.RelativeTable;
                txtRelativeColumnName.Text = PropertyEntity.RelativeColumn;
                txtRelativeSearchColumnName.Text = PropertyEntity.RelativeSearchColumn;
                chkRequireRemark.Checked = PropertyEntity.IfRequireRemark;
                LoadPropertyTypePanel(PropertyEntity);
            }
        }


        private void LoadPropertyTypePanel(ReportPropertyEntity PropertyEntity)
        {
            if (PropertyEntity.PropertyType == DictionaryCategoryConst.GridColumnType.DictionaryItem)
            {

                dplPulicCategory.Visible = true;
            }
            else
            {
                dplPulicCategory.Visible = false;
            }

            if (PropertyEntity.PropertyType == DictionaryCategoryConst.GridColumnType.RelativeTable)
            {
                panelRelativeTable.Visible = true;
            }
            else
            {
                panelRelativeTable.Visible = false;
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var entity = new ReportPropertyEntity(CurrentPropertyID);
            if (entity.IsNew)
            {
                entity.ReportCategoryId = CurrentReportCategoryID;
            }
            entity.PropertyName = txtPropertyName.Text.Trim();
            entity.Title = txtHeaderText.Text;

            entity.PropertyType = int.Parse(dplPropertyType.SelectedValue);
            if (dplPulicCategory.SelectedValue.Trim().Length>0)
            {
                entity.PropertyDictCategoryId = int.Parse(dplPulicCategory.SelectedValue);
            }
            entity.RelativeTable = txtRelativeTableName.Text.Trim();
            entity.RelativeColumn = txtRelativeColumnName.Text.Trim();
            entity.RelativeSearchColumn = txtRelativeSearchColumnName.Text.Trim();
            entity.IfRequireRemark = chkRequireRemark.Checked;
           
            if (entity.Save())
            {
                RediretResolveUrl(SiteUrls.AdminTemplate.ReportPropertyList+"?ReportCategoryID="+CurrentReportCategoryID);
            }
            else
            {
                Alert(GenerateAlert.SaveFailed);
            }

        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            RediretResolveUrl(SiteUrls.AdminTemplate.ReportPropertyList + "?ReportCategoryID=" + CurrentReportCategoryID);
        }
    }
}
